package com.iflytek.web.sevlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.iflytek.domain.BookedHome;
import com.iflytek.domain.Homes;
import com.iflytek.domain.Hometype;
import com.iflytek.domain.Orderitem;
import com.iflytek.domain.Orders;
import com.iflytek.domain.User;
import com.iflytek.service.OrderService;
import com.iflytek.utils.AutoCreateIdUtils;


@WebServlet("/order")
public class OrderServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	
	public void addIntoOrder(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		Orders orders=new Orders();
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		OrderService os=(OrderService) ac.getBean("orderService");
		
		try {
			BeanUtils.populate(orders, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		System.out.println("qqq  "+orders);
		os.insertOrders(orders);
		System.out.println("添加到订单");
		
	}
	//立即预定
	public void immediateBooking(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		Orders orders=new Orders();
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		OrderService os=(OrderService) ac.getBean("orderService");
		try {
			BeanUtils.populate(orders, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		user=os.getUserByPhoneNumber(user.getUserphone());
		String uid=user.getUid();
		Integer state=0;
		String startDtae=request.getParameter("startDtae");
		String startHour=request.getParameter("startHour");
		String stopDtae=request.getParameter("stopDtae");
		String stopHour=request.getParameter("stopHour");
		String htid=request.getParameter("htid");
		Hometype hometype=os.getHometypeByHtid(htid);
		String oid=AutoCreateIdUtils.getOID();
        Timestamp startTime = Timestamp.valueOf(startDtae+" "+startHour);  
        Timestamp stopTime = Timestamp.valueOf(stopDtae+" "+stopHour); 
        orders.setOid(oid);
        orders.setStartTime(startTime);
        orders.setStopTime(stopTime);
        orders.setState(state);
        orders.setUid(uid);
        orders.setUser(user);
        orders.setHtid(htid);
        orders.setHometype(hometype);
        os.insertOrders(orders);
        
        request.setAttribute("order",orders);
        
       /* Orderitem orderitems=new Orderitem();
        List<Orders> orderList=new ArrayList<Orders>();
        orderList.add(orders);
        orderitems.setOrders(orderList);
		System.out.println("添加到订单");
		request.setAttribute("orderitems", orderitems);*/
		request.getRequestDispatcher("my_order.jsp").forward(request, response);
		
	}

	//立即预定
	public void creatOrderitem(HttpServletRequest request, HttpServletResponse response,Orders orders) 
			throws ServletException, IOException {
		Orderitem orderitem=new Orderitem();
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		OrderService os=(OrderService) ac.getBean("orderService");
		
		System.out.println("添加到订单");
		request.getRequestDispatcher("my_order.jsp").forward(request, response);
		
	}

	//获取可被预定的房间集合
	public void getCanBookHomes(HttpServletRequest request, HttpServletResponse response,Orders orders) 
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		OrderService os=(OrderService) ac.getBean("orderService");
		String orderId=request.getParameter("");
		Orders order=os.getOrderByOid(orderId);
		String htid=order.getHtid();
		List<BookedHome> bookedHomeList=os.getBookedHomeByHtid(htid);
		List<Homes> homeList =os.getHomeByHtid(htid);
		List<Homes> canBookHomeList=os.getCanBookHomeList(order,bookedHomeList,homeList);
		
		
	}

	
}
